﻿#include "stdafx.h"
#include "ToolScriptBinder.h"
#include "DebugAssert.h"

_NAMESPACE_BEGIN

template<typename T>
static void SaveLog_Wrp(T info)
{
	Log::Instance()->SaveLog(info);
}

template<typename T>
static void SaveLogNoEndl_Wrp(T info)
{
	Log::Instance()->SaveLog(info,false);
}

static void SaveLogUTF8_Wrp(CONST_REF(std::string) info)
{
	wstring utf;
	CodeConvert::Instance()->UTF8_2Unicode(info.c_str(),utf);
	
	string log;
	CodeConvert::Instance()->Unicode2MultiByte(utf.c_str(),log);
	Log::Instance()->SaveLog(log);
}

static void SaveLogUTF8NoEndl_Wrp(CONST_REF(std::string) info)
{
	wstring utf;
	CodeConvert::Instance()->UTF8_2Unicode(info.c_str(),utf);

	string log;
	CodeConvert::Instance()->Unicode2MultiByte(utf.c_str(),log);
	Log::Instance()->SaveLog(log,false);
}

Tool_ScriptBinder::Tool_ScriptBinder()
{
	asIScriptEngine* engine=AngelScriptBinder::Instance()->GetEngine();

	int r;

	///SoundEffect
	r = engine->RegisterObjectType("SoundEffect", sizeof(SoundEffect),asOBJ_REF); assert( r >= 0 );
	DECLARE_EMPTY_ADD_RELEASE_REF(SoundEffect)
	TYPEMETHODS(SoundEffect)

	///SoundTool
	r = engine->RegisterObjectType("SoundTool",sizeof(SoundTool), asOBJ_REF|asOBJ_NOHANDLE );	assert( r >= 0 );
	TYPEMETHODS(SoundTool)
	r = engine->RegisterGlobalProperty("SoundTool SoundTool_I",SoundTool::Instance() );	 assert( r >= 0 );

	///Log
	r=engine->RegisterGlobalFunction("void SAVE_LOG(string&in)",asFUNCTION(SaveLogUTF8_Wrp), asCALL_CDECL);	 assert( r >= 0 );
	r=engine->RegisterGlobalFunction("void SAVE_LOG(int)",asFUNCTION(SaveLog_Wrp<int>), asCALL_CDECL);	 assert( r >= 0 );
	r=engine->RegisterGlobalFunction("void SAVE_LOG(float)",asFUNCTION(SaveLog_Wrp<float>), asCALL_CDECL);	 assert( r >= 0 );
	r=engine->RegisterGlobalFunction("void SAVE_LOG(bool)",asFUNCTION(SaveLog_Wrp<bool>), asCALL_CDECL);	 assert( r >= 0 );
	r=engine->RegisterGlobalFunction("void SAVE_LOG_NENDL(string&in)",asFUNCTION(SaveLogUTF8NoEndl_Wrp), asCALL_CDECL);	 assert( r >= 0 );
	r=engine->RegisterGlobalFunction("void SAVE_LOG_NENDL(int)",asFUNCTION(SaveLogNoEndl_Wrp<int>), asCALL_CDECL);	 assert( r >= 0 );
	r=engine->RegisterGlobalFunction("void SAVE_LOG_NENDL(float)",asFUNCTION(SaveLogNoEndl_Wrp<float>), asCALL_CDECL);	 assert( r >= 0 );
	r=engine->RegisterGlobalFunction("void SAVE_LOG_NENDL(bool)",asFUNCTION(SaveLogNoEndl_Wrp<bool>), asCALL_CDECL);	 assert( r >= 0 );
}

_NAMESPACE_END

